package com.sdy;

import com.alibaba.fastjson.JSONObject;
import com.sdy.po.Parm;
import com.sdy.po.SerialPO;
import com.sdy.window.MainWindow;
import com.sdy.utils.SerialPortUtils;
import com.sdy.utils.MyWebSocketClient;
import org.java_websocket.enums.ReadyState;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 核心类
 */
public class SerialFactory {

    public static List<SerialPO> serialList;

    public List<SerialPortUtils> serialPortList = new ArrayList<SerialPortUtils>();

    private MainWindow mainWindow;

    private SerialPortUtils serialPort ;

    public static MyWebSocketClient webSocketClient ;


    public SerialPortUtils getSerialPort() {
        return serialPort;
    }

    public void setSerialPort(SerialPortUtils serialPort) {
        this.serialPort = serialPort;
    }

    public MainWindow getMainWindow() {
        return mainWindow;
    }

    public void setMainWindow(MainWindow mainWindow) {
        this.mainWindow = mainWindow;
    }

    public List<SerialPO> getSerialList() {
        return serialList;
    }

    public void setSerialList(List<SerialPO> serialList) {
        this.serialList = serialList;
    }

    public MyWebSocketClient getWebSocketClient() {
        return webSocketClient;
    }

    public void setWebSocketClient(MyWebSocketClient webSocketClient) {
        System.out.println("将websocket连接对象放入factory");
        this.webSocketClient = webSocketClient;
    }

    public SerialFactory() {
    }

//    public void showStartProcess(String msg){
//        mainWindow.getJ3().setText(msg);
//    }

    public SerialFactory(List<SerialPO> serialList, MainWindow mainWindow, SerialPortUtils serialPort, MyWebSocketClient webSocketClient) {
        this.serialList = serialList;
        this.mainWindow = mainWindow;
        this.serialPort = serialPort;
        this.webSocketClient = webSocketClient;
    }

    //初始化websocket
    public Boolean initWebSocket(){
        System.out.println("与服务器建立连接......");
//        showStartProcess("与服务器建立连接......");
        webSocketClient.connect();
        Integer num = 0;
        while (webSocketClient.getReadyState() != ReadyState.OPEN) {
            System.out.println("连接状态：" + webSocketClient.getReadyState());
            try {
                if (num>2){
                    return false;
                }
                num++;
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    //初始化所有串口
    public List<JSONObject> initSerials(){
        System.out.println("正在初始化串口......");
        List<JSONObject> resultList = new ArrayList<JSONObject>();
        for (SerialPO serial:serialList) {
            serialPort = new SerialPortUtils();
            JSONObject jsonObject = serialPort.initSerial(serial);
            resultList.add(jsonObject);
            if ((Boolean)jsonObject.get("result")){
                serialPortList.add(serialPort);
            }
        }
        return resultList;
    }

    //开放读取串口
    public void closeAllSerial(){
        for (SerialPortUtils serialPortUtils:serialPortList) {
            serialPortUtils.closeSerialPort();
        }
    }

    //向服务器发送消息
    public static Boolean sendMessage(Parm parm, String data, Boolean idHandled, Integer comopanyId){
        JSONObject message = new JSONObject();
        String[] dataString = data.split("=");
        message.put("time",new Date().toString());
        message.put("data",Double.valueOf(dataString[1]));
        message.put("parm",parm);
        message.put("isHandled",idHandled);
        message.put("companyId",comopanyId);
        System.out.println(message.toJSONString());
        webSocketClient.send(message.toJSONString());
        message.clear();
        return true;
    }



}
